<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>HTTP context 的选项列表</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="context.socket.html">套接字上下文选项</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="context.ftp.html">FTP context options</a></div>
 <div class="up"><a href="context.html">上下文（Context）选项和参数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="context.http" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">HTTP context 选项</h1>
  <p class="refpurpose"><span class="refname">HTTP context 选项</span> &mdash; <span class="dc-title">HTTP context 的选项列表</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-context.http-description">
  <h3 class="title">说明</h3>
  <p class="para">
   提供给 <em>http://</em> 和 <em>https://</em> 传输协议的 context 选项。
   transports.
  </p>
 </div>


 <div class="refsect1 options" id="refsect1-context.http-options">
  <h3 class="title">可选项</h3>
  <p class="para">
   <dl>

    
     <dt id="context.http.method">
      <em><code class="parameter">method</code></em>
      <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
     </dt>

     <dd>

      <p class="para">
       远程服务器支持的 <strong><code>GET</code></strong>，<strong><code>POST</code></strong> 或其它 HTTP 方法。
      </p>
      <p class="para">
       默认值是 <strong><code>GET</code></strong>。
      </p>
     </dd>

    
    
     <dt id="context.http.header">
      <em><code class="parameter">header</code></em>
      <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
     </dt>

     <dd>

      <p class="para">
       请求期间发送的额外 header 。在此选项的值将覆盖其他值
       （诸如 <em>User-agent:</em>， <em>Host:</em> 和 
       <em>Authentication:</em>）。
      </p>
     </dd>

    
    
     <dt id="context.http.user-agent">
      <em><code class="parameter">user_agent</code></em>
      <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
     </dt>

     <dd>

      <p class="para">
       要发送的 header <em>User-Agent:</em> 的值。如果在上面的 
       <em>header</em> context 选项中没有指定 user-agent，此值将被使用。
      </p>
      <p class="para">
       默认使用 <var class="filename">php.ini</var>  中设置的 <a href="filesystem.configuration.html#ini.user-agent" class="link">user_agent</a>。
      </p>
     </dd>

    
    
     <dt id="context.http.content">
      <em><code class="parameter">content</code></em>
      <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
     </dt>

     <dd>

      <p class="para">
       在 header 后面要发送的额外数据。通常使用POST或PUT请求。
      </p>
     </dd>

    
    
     <dt id="context.http.proxy">
      <em><code class="parameter">proxy</code></em>
      <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
     </dt>

     <dd>

      <p class="para">
       URI 指定的代理服务器的地址。(e.g.
       <em>tcp://proxy.example.com:5100</em>).
      </p>
     </dd>

    
    
     <dt id="context.http.request-fulluri">
      <em><code class="parameter">request_fulluri</code></em>
      <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>
     </dt>

     <dd>

      <p class="para">
       当设置为 <strong><code>TRUE</code></strong> 时，在构建请求时将使用整个 URI 。(i.e.
       <em>GET http://www.example.com/path/to/file.html HTTP/1.0</em>)。
       虽然这是一个非标准的请求格式，但某些代理服务器需要它。
      </p>
      <p class="para">
       默认值是 <strong><code>FALSE</code></strong>.
      </p>
     </dd>

    
    
     <dt id="context.http.follow-location">
      <em><code class="parameter">follow_location</code></em>
      <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>
     </dt>

     <dd>

      <p class="para">
       跟随 <em>Location</em> header 的重定向。设置为 <em>0</em> 以禁用。
      </p>
      <p class="para">
       默认值是 <em>1</em>。
      </p>
     </dd>

    
    
     <dt id="context.http.max-redirects">
      <em><code class="parameter">max_redirects</code></em>
      <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>
     </dt>

     <dd>

      <p class="para">
       跟随重定向的最大次数。值为 <em>1</em> 或更少则意味不跟随重定向。
      </p>
      <p class="para">
       默认值是 <em>20</em>。
      </p>
     </dd>

    
    
     <dt id="context.http.protocol-version">
      <em><code class="parameter">protocol_version</code></em>
      <span class="type"><a href="language.types.float.html" class="type float">float</a></span>
     </dt>

     <dd>

      <p class="para">
       HTTP 协议版本。
      </p>
      <p class="para">
       默认值是 <em>1.0</em>。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        PHP 5.3.0 以前的版本没有实现分块传输解码。
        如果此值设置为 <em>1.1</em> ，与 <em>1.1</em> 的兼容将是你的责任。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt id="context.http.timeout">
      <em><code class="parameter">timeout</code></em>
      <span class="type"><a href="language.types.float.html" class="type float">float</a></span>
     </dt>

     <dd>

      <p class="para">
       读取超时时间，单位为秒（s），用 <span class="type"><a href="language.types.float.html" class="type float">float</a></span> 指定(e.g. <em>10.5</em>)。
      </p>
      <p class="para">
       默认使用 <var class="filename">php.ini</var> 中设置的 <a href="filesystem.configuration.html#ini.default-socket-timeout" class="link">default_socket_timeout</a>。
      </p>
     </dd>

    
    
     <dt id="context.http.ignore-errors">
      <em><code class="parameter">ignore_errors</code></em>
      <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>
     </dt>

     <dd>

      <p class="para">
       即使是故障状态码依然获取内容。
      </p>
      <p class="para">
       默认值为 <strong><code>FALSE</code></strong>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-context.http-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.4</td>
       <td>
        添加 <em><code class="parameter">follow_location</code></em>。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        当 <em><code class="parameter">protocol_version</code></em> 设置为 <em>1.1</em> 时支持分块传输解码。
       </td>
      </tr>

      <tr>
       <td>5.2.10</td>
       <td>
        添加 <em><code class="parameter">ignore_errors</code></em>。
       </td>
      </tr>

      <tr>
       <td>5.2.10</td>
       <td>
        <em><code class="parameter">header</code></em> 现在可以是一个数字索引的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>。
       </td>
      </tr>

      <tr>
       <td>5.2.1</td>
       <td>
        添加 <em><code class="parameter">timeout</code></em>。
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        Added HTTPS proxying through HTTP proxies.
        添加经由 HTTP 代理的 HTTPS 代理。
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        添加 <em><code class="parameter">max_redirects</code></em>。
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        添加 <em><code class="parameter">protocol_version</code></em>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-context.http-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="context.http.example-post">
    <p><strong>Example #1 获取一个页面并发送 POST 数据</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$postdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'var1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'some&nbsp;content'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'var2'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'doh'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'http'&nbsp;</span><span style="color: #007700">=&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'POST'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'header'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Content-type:&nbsp;application/x-www-form-urlencoded'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'content'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$postdata<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://example.com/submit.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="context.http.example-fetch-ignore-redirect">
    <p><strong>Example #2 忽略重定向并获取 header 和内容</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"http://www.example.org/header.php"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'http'&nbsp;</span><span style="color: #007700">=&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'GET'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'max_redirects'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ignore_errors'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'1'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;header&nbsp;information&nbsp;as&nbsp;well&nbsp;as&nbsp;meta&nbsp;data<br />//&nbsp;about&nbsp;the&nbsp;stream<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;actual&nbsp;data&nbsp;at&nbsp;$url<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-context.http-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Underlying socket stream context options</strong><br />
   <span class="simpara">
    Additional context options may be supported by the
    <a href="transports.inet.html" class="link">underlying transport</a>
    For <em>http://</em> streams, refer to context
    options for the <em>tcp://</em> transport.  For
    <em>https://</em> streams, refer to context options
    for the <em>ssl://</em> transport.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>HTTP status line</strong><br />
   <span class="simpara">
    When this stream wrapper follows a redirect, the
    <em>wrapper_data</em> returned by
    <span class="function"><a href="function.stream-get-meta-data.html" class="function">stream_get_meta_data()</a></span> might not necessarily contain
    the HTTP status line that actually applies to the content data at index
    <em>0</em>.
   </span>
   <div class="example-contents screen">
<div class="cdata"><pre>
array (
  &#039;wrapper_data&#039; =&gt;
  array (
    0 =&gt; &#039;HTTP/1.0 301 Moved Permantenly&#039;,
    1 =&gt; &#039;Cache-Control: no-cache&#039;,
    2 =&gt; &#039;Connection: close&#039;,
    3 =&gt; &#039;Location: http://example.com/foo.jpg&#039;,
    4 =&gt; &#039;HTTP/1.1 200 OK&#039;,
    ...
</pre></div>
   </div>
   <span class="simpara">
    The first request returned a <em>301</em> (permanent redirect),
    so the stream wrapper automatically followed the redirect to get a
    <em>200</em> response (index = <em>4</em>).
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-context.http-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="wrappers.http.html" class="xref">http://</a></li>
    <li class="member"><a href="context.socket.html" class="xref">套接字上下文选项</a></li>
    <li class="member"><a href="context.ssl.html" class="xref">SSL context options</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="context.socket.html">套接字上下文选项</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="context.ftp.html">FTP context options</a></div>
 <div class="up"><a href="context.html">上下文（Context）选项和参数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
